//
//  HomeViewController.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HomeViewController.h"
#import "LykDaoHangView.h"
#import "HomeCollectionViewCell.h"
#import "HomeCollectionViewCellTwo.h"
#import "DataBaseHandle.h"
@interface HomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, jump, fresh, pushPage>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *daoHangArray;
@property (nonatomic, strong) NSMutableArray *showArray;
@property (nonatomic, strong) NSMutableArray *tiquDaoHangArray;
@property (nonatomic, strong) LykDaoHangView *daoHangView;
@property (nonatomic, copy) NSString *url;
@end

@implementation HomeViewController


-(void)viewWillAppear:(BOOL)animated {
    
    [[DataBaseHandle shareDataBase] openDataBase];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
    } else {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
    }
//    NSIndexPath *indext = [NSIndexPath indexPathForItem:self.collectionView.contentOffset.x / self.view.width inSection:0];
//    HomeCollectionViewCell *cell = (HomeCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indext];
//    [cell.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"礼物说";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.daoHangArray = [[NSMutableArray alloc] initWithCapacity:0];
    self.showArray = [NSMutableArray arrayWithCapacity:0];
    
//    导航
    self.daoHangView = [[LykDaoHangView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.width / 10.3)];
    self.daoHangView.delegate = self;
    [self.daoHangView getblock:^(NSInteger a) {
        [self.collectionView setContentOffset:CGPointMake(self.view.width * a, 0) animated:YES];
    }];
    [self.view addSubview:self.daoHangView];
    [LYKNetWorkTool getWithUrl:KHomeDaoHang parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *allDic = [[NSDictionary alloc] initWithDictionary:result];
        NSDictionary *bannerDic = allDic[@"data"];
        NSArray *AllArray = bannerDic[@"candidates"];
        for (NSDictionary *dic in AllArray) {
            HomeDaoHangModel *model = [[HomeDaoHangModel alloc] initWithDictionary:dic];
            [self.daoHangArray addObject:model];
        }
        HomeDaoHangModel *model = [[HomeDaoHangModel alloc] init];
        model.name = @"精选";
        [self.daoHangArray insertObject:model atIndex:0];
        
        self.tiquDaoHangArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (HomeDaoHangModel *model in self.daoHangArray) {
            [self.tiquDaoHangArray addObject:model.name];
        }
        [self compareArray];
        self.daoHangView.daoHangArray = self.tiquDaoHangArray;
        [self.collectionView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
//    collectionview
    UICollectionViewFlowLayout *floelayout = [[UICollectionViewFlowLayout alloc] init];
    floelayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    floelayout.itemSize = CGSizeMake(self.view.width, self.view.height - 108 - self.daoHangView.height);
    floelayout.minimumInteritemSpacing = 0;
    floelayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.daoHangView.height, self.view.width, self.view.height - 108 - self.daoHangView.height) collectionViewLayout:floelayout];
    self.collectionView.backgroundColor = [UIColor blackColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    [self.collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
    [self.collectionView registerClass:[HomeCollectionViewCellTwo class] forCellWithReuseIdentifier:@"HomeCollectionViewCellTwo"];
    [self.view addSubview:self.collectionView];
    
    [self.view bringSubviewToFront:self.daoHangView];
}



#pragma mark -- collectionview协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.showArray.count > 0) {
        return self.showArray.count;
    }
    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.backgroundColor = [UIColor greenColor];
        return cell;
    } else {
        HomeCollectionViewCellTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCellTwo" forIndexPath:indexPath];
        cell.idUrl = [self.showArray[indexPath.item ] ids];
        cell.delegate = self;
        return cell;
        
    }
    
}

#pragma mark -- 跳转协议
-(void)jump:(NSString *)url {
    WebViewController *web = [[WebViewController alloc] init];
    web.type = @"post";
    web.url = [NSString stringWithFormat:@"%@%@", kPostInfo, url];
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark -- 偏移关联
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.daoHangView.page = scrollView.contentOffset.x / self.view.width;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.daoHangView.x = scrollView.contentOffset.x / scrollView.contentSize.width;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.daoHangView.x = scrollView.contentOffset.x / scrollView.contentSize.width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 对比本地数组
-(void)compareArray {
    [self.showArray removeAllObjects];
    NSArray *array = nil;
    if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"array"] isEqualToString:@"1"]) {
        array = [[NSArray alloc] initWithArray:[self getArray]];
    }else {
        array = [[NSArray alloc] initWithArray:self.tiquDaoHangArray];
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
        NSString *newFielPath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
        [array writeToFile:newFielPath atomically:YES];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"array"];
    }
    
    for (int j = 0; j < array.count; j++) {
        for (int i = 0; i < self.daoHangArray.count; i++) {
            if ([[self.daoHangArray[i] name] isEqualToString:array[j]]) {
                [self.showArray addObject:self.daoHangArray[i]];
            }
        }
    }
}

#pragma mark -- 刷新数组
-(void)freshArray {
    [self compareArray];
    [self.collectionView reloadData];
}

#pragma mark -- 本地取数组
-(NSArray *)getArray {
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
    NSLog(@"%@", documentsPath);
    NSString *newFielPath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
    return [NSArray arrayWithContentsOfFile:newFielPath];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
